package com.chen.nongansampling.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import androidx.fragment.app.FragmentManager;

import com.chen.nongansampling.R;
import com.chen.nongansampling.bean.notice.NoticeBean;
import com.chen.nongansampling.fragment.WebviewFragment;

import java.util.List;

public class NoticeListviewAdapter extends BaseAdapter
{
    private List<NoticeBean> list;
    private Context activity;
    private FragmentManager f;

    public NoticeListviewAdapter(List<NoticeBean> list, Context activity, FragmentManager fragmentManager) {
        this.list=list;
        this.activity=activity;
        this.f=fragmentManager;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

//        if (list.get(position).getComplicated()!=null&&list.get(position).getComplicated()==true)
//        {
//            Log.d("LayoutInflater","true");
//            View view = LayoutInflater.from(activity).inflate(R.layout.listview_rich_text_item, null);
////            ImageView imageView = (ImageView) view.findViewById(R.id.listview_text_image);
////            TextView data = (TextView) view.findViewById(R.id.listview_text_data);
////            TextView name = (TextView) view.findViewById(R.id.listview_text_name);
////            imageView.setImageResource(list.get(position).getImageSrc());
////            data.setText((String) list.get(position).getData());
////            name.setText(list.get(position).getName());
//            return view;
//
//        }else {
            View view = LayoutInflater.from(activity).inflate(R.layout.notice_listview_item, null);
    //        ImageView imageView = (ImageView) view.findViewById(R.id.listview_text_image);
            TextView title = (TextView) view.findViewById(R.id.notice_left_text);
            TextView time = (TextView) view.findViewById(R.id.notice_right_text);
    //        imageView.setImageResource(list.get(position).getImageSrc());
        if (list.get(position).getTitle()!=null)
        title.setText((String) list.get(position).getTitle());
//        time.setText(list.get(position).getTitle());
        time.setText(list.get(position).getCreatedDateTime().split("T")[0]);

        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                f.beginTransaction()
                        .add(R.id.container, WebviewFragment.newInstance(list.get(position).getContent(),list.get(position).getTitle()))
//                .addToBackStack("SampleFragment")
                        .commitNow();
            }
        });

            return view;
        }

        //   return null;
    }
